import Vue from 'vue'
import VueRouter from 'vue-router'
import Article from '@/views/ArticleDetail.vue'
import HomePage from '@/views/HomePage.vue'
import QuestionPage from '@/views/QuestionPage.vue'
import UserPage from '@/views/UserPage.vue'
import VideoPage from '@/views/VideoPage.vue'
// import Tabbar from '@/components/App-tabbar.vue'
// import ArticleItem from '@/components/Article-item.vue'
// import Tabbar from '@/components/App-tabbar.vue'
import Login from '@/views/LoginPage.vue'
import ProfilePage from '@/views/ProfilePage.vue'
import { Toast } from 'vant'
import { getToken } from '@/utils/storage'

// import store from '@/store/index.js'

Vue.use(VueRouter)

const routes = [
  { path: '/article', component: Article },
  { path: '/', component: HomePage },
  { path: '/question', component: QuestionPage },
  { path: '/user', component: UserPage },
  { path: '/video', component: VideoPage },
  { path: '/login', component: Login },
  { path: '/user/profile', component: ProfilePage }
]

const router = new VueRouter({
  routes,
  mode: 'history'
})

// 前置守卫
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  // 1.to   你往哪里去
  // 2.from 你从哪里来
  // 3.next() 是否放行
  // (localStorage.getItem('token-jky') || '')
  const token = getToken()
  if (token) {
    next()
  } else {
    // console.log(store)
    if (whiteList.includes(to.path)) {
      // console.log(1111)
      next()
    } else {
      Toast.fail('您还未等登录,请登录')
      next('/login')
    }
  }
})

export default router
